/* 
* 地图自动缩放功能：
* 地图备注：（中间没有空格）
* <Camera Scale:x>
* x：缩放比例（正数）
* 放大或缩小均可。
* 
* 修正了缩小镜头背景显示不全的问题。
* 修正像素移动目的地坐标错误问题。（在SOUL_MV Destination Cursor中修正）
* 修正放大镜头后在地图边缘无法移动镜头的问题。
* 隐藏原本未隐藏的飞船阴影（原本藏在镜头外，会因镜头缩小而现显示出来）
*/

var SWDA_MapCameraScale = SWDA_MapCameraScale || {};

// 地图加载时的效果
SWDA_MapCameraScale.DataManager_isMapLoaded = DataManager.isMapLoaded;
DataManager.isMapLoaded = function () {
  if (!SWDA_MapCameraScale.DataManager_isMapLoaded.call(this))
    return false;

  LoadMapManager.loadMapCamScale();
  return true;
};

LoadMapManager.loadMapCamScale = function () {
  if ($dataMap.meta && $dataMap.meta["Camera Scale"]) {
    var camScale = $dataMap.meta["Camera Scale"];
    $gameSystem._drill_LCa_sX.move = 0;
    $gameSystem._drill_LCa_sX.time = 1;
    $gameSystem._drill_LCa_sX.speed = (Number(camScale) - 1 - $gameSystem._drill_LCa_sX.cur) / $gameSystem._drill_LCa_sX.time;
    $gameSystem._drill_LCa_sY.move = 0;
    $gameSystem._drill_LCa_sY.time = 1;
    $gameSystem._drill_LCa_sY.speed = (Number(camScale) - 1 - $gameSystem._drill_LCa_sY.cur) / $gameSystem._drill_LCa_sY.time;
  } else {
    $gameSystem._drill_LCa_sX.move = 0;
    $gameSystem._drill_LCa_sX.time = 1;
    $gameSystem._drill_LCa_sX.speed = (0 - $gameSystem._drill_LCa_sX.cur) / $gameSystem._drill_LCa_sX.time;
    $gameSystem._drill_LCa_sY.move = 0;
    $gameSystem._drill_LCa_sY.time = 1;
    $gameSystem._drill_LCa_sY.speed = (0 - $gameSystem._drill_LCa_sY.cur) / $gameSystem._drill_LCa_sY.time;
  }

};



//==============================
// * 修正放大后的镜头移动范围【全部覆写】
//==============================
Game_Map.prototype.scrollDown = function (distance) {
  distance = Number(distance.toFixed(6))


  var lastY = this._displayY;
  var scaleDiffY = Math.floor(Graphics.height * (1 / this.tileHeight() -
    1 / (this.tileHeight() * (1 + $gameSystem._drill_LCa_sY.cur))) / 2);    //缩放后活动范围修正
  if (this.isLoopVertical()) {
    this._displayY += distance;
    this._displayY %= $dataMap.height;
    if (this._parallaxLoopY) {
      this._parallaxY += distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    this._displayY = Math.min(this._displayY + distance,
      this.height() - this.screenTileY() + scaleDiffY);    //缩放后活动范围修正
    this._parallaxY += this._displayY - lastY;
  }
  // Sleipnir Mapping Overlay System 1：sunlight层
  this.reclaimsunlight();
  if (this.isLoopVertical()) {
    if (this._sunlightLoopY) {
      this._sunlightY += distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    var displayY = Math.min(lastY + distance,
      this.height() - this.screenTileY() + scaleDiffY);    //缩放后活动范围修正
    this._sunlightY += displayY - lastY;
  }
  // Sleipnir Mapping Overlay System 2：overhead层
  this.reclaimoverhead();
  if (this.isLoopVertical()) {
    if (this._overheadLoopY) {
      this._overheadY += distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    var displayY = Math.min(lastY + distance,
      this.height() - this.screenTileY() + scaleDiffY);    //缩放后活动范围修正
    this._overheadY += displayY - lastY;
  }
  //patch_MapOverlay
  if (this.height() >= this.screenTileY()) {
    SceneManager._scene.fogImage.origin.y +=
      ((this._displayY + distance + this.screenTileY()) < (this.height() + scaleDiffY) ? distance : 0) *
      this.tileHeight() * (1 + $gameSystem._drill_LCa_sX.cur);    //缩放后活动范围修正
  }

};

Game_Map.prototype.scrollLeft = function (distance) {
  distance = Number(distance.toFixed(6))

  var lastX = this._displayX;
  var scaleDiffX = Math.floor(Graphics.width * (1 / this.tileWidth() -
    1 / (this.tileWidth() * (1 + $gameSystem._drill_LCa_sX.cur))) / 2);    //缩放后活动范围修正
  if (this.isLoopHorizontal()) {
    this._displayX += $dataMap.width - distance;
    this._displayX %= $dataMap.width;
    if (this._parallaxLoopX) {
      this._parallaxX -= distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    this._displayX = Math.max(this._displayX - distance, 0 - scaleDiffX);    //缩放后活动范围修正
    this._parallaxX += this._displayX - lastX;
  }
  // Sleipnir Mapping Overlay System 1：sunlight层
  this.reclaimsunlight();
  if (this.isLoopHorizontal()) {
    if (this._sunlightLoopX) {
      this._sunlightX -= distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    var displayX = Math.max(lastX - distance, 0 - scaleDiffX);    //缩放后活动范围修正
    this._sunlightX += displayX - lastX;
  }
  // Sleipnir Mapping Overlay System 2：overhead层
  this.reclaimoverhead();
  if (this.isLoopHorizontal()) {
    if (this._overheadLoopX) {
      this._overheadX -= distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    var displayX = Math.max(lastX - distance, 0 - scaleDiffX);    //缩放后活动范围修正
    this._overheadX += displayX - lastX;
  }
  //patch_MapOverlay
  if (this.width() >= this.screenTileX()) {
    SceneManager._scene.fogImage.origin.x -=
      ((this._displayX - distance) > (0 - scaleDiffX) ? distance : 0) *
      this.tileWidth() * (1 + $gameSystem._drill_LCa_sX.cur);    //缩放后活动范围修正
  }
};

Game_Map.prototype.scrollRight = function (distance) {
  distance = Number(distance.toFixed(6))

  var lastX = this._displayX;
  var scaleDiffX = Math.floor(Graphics.width * (1 / this.tileWidth() -
    1 / (this.tileWidth() * (1 + $gameSystem._drill_LCa_sX.cur))) / 2);    //缩放后活动范围修正
  if (this.isLoopHorizontal()) {
    this._displayX += distance;
    this._displayX %= $dataMap.width;
    if (this._parallaxLoopX) {
      this._parallaxX += distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    this._displayX = Math.min(this._displayX + distance,
      this.width() - this.screenTileX() + scaleDiffX);    //缩放后活动范围修正
    this._parallaxX += this._displayX - lastX;
  }
  // Sleipnir Mapping Overlay System 1：sunlight层
  this.reclaimsunlight();
  if (this.isLoopHorizontal()) {
    if (this._sunlightLoopX) {
      this._sunlightX += distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    var displayX = Math.min(lastX + distance,
      this.width() - this.screenTileX() + scaleDiffX);    //缩放后活动范围修正
    this._sunlightX += displayX - lastX;
  }
  // Sleipnir Mapping Overlay System 2：overhead层
  this.reclaimoverhead();
  if (this.isLoopHorizontal()) {
    if (this._overheadLoopX) {
      this._overheadX += distance;
    }
  } else if (this.width() >= this.screenTileX()) {
    var displayX = Math.min(lastX + distance,
      this.width() - this.screenTileX() + scaleDiffX);    //缩放后活动范围修正
    this._overheadX += displayX - lastX;
  }
  //patch_MapOverlay
  if (this.width() >= this.screenTileX()) {
    SceneManager._scene.fogImage.origin.x +=
      ((this._displayX + distance + this.screenTileX()) < (this.width() + scaleDiffX) ? distance : 0) *
      this.tileWidth() * (1 + $gameSystem._drill_LCa_sX.cur);    //缩放后活动范围修正
  }
};

Game_Map.prototype.scrollUp = function (distance) {
  distance = Number(distance.toFixed(6))
  var lastY = this._displayY;
  var scaleDiffY = Math.floor(Graphics.height * (1 / this.tileHeight() -
    1 / (this.tileHeight() * (1 + $gameSystem._drill_LCa_sY.cur))) / 2);    //缩放后活动范围修正
  if (this.isLoopVertical()) {
    this._displayY += $dataMap.height - distance;
    this._displayY %= $dataMap.height;
    if (this._parallaxLoopY) {
      this._parallaxY -= distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    this._displayY = Math.max(this._displayY - distance, 0 - scaleDiffY);    //缩放后活动范围修正
    this._parallaxY += this._displayY - lastY;
  }
  // Sleipnir Mapping Overlay System 1：sunlight层
  this.reclaimsunlight();
  if (this.isLoopVertical()) {
    if (this._sunlightLoopY) {
      this._sunlightY -= distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    var displayY = Math.max(lastY - distance, 0 - scaleDiffY);    //缩放后活动范围修正
    this._sunlightY += displayY - lastY;
  }
  // Sleipnir Mapping Overlay System 2：overhead层
  this.reclaimoverhead();
  if (this.isLoopVertical()) {
    if (this._overheadLoopY) {
      this._overheadY -= distance;
    }
  } else if (this.height() >= this.screenTileY()) {
    var displayY = Math.max(lastY - distance, 0 - scaleDiffY);    //缩放后活动范围修正
    this._overheadY += displayY - lastY;
  }
  //patch_MapOverlay
  if (this.height() >= this.screenTileY()) {
    SceneManager._scene.fogImage.origin.y -=
      ((this._displayY - distance) > (0 - scaleDiffY) ? distance : 0) *
      this.tileHeight() * (1 + $gameSystem._drill_LCa_sX.cur);    //缩放后活动范围修正
  }
};




//==============================
// * 远景图更新（避免缩小时显示不全）
//==============================
SWDA_MapCameraScale.Scene_Map_Update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
  SWDA_MapCameraScale.Scene_Map_Update.call(this);

  var _x = Graphics.boxWidth / this._spriteset.scale.x - Graphics.boxWidth;
  var _y = Graphics.boxHeight / this._spriteset.scale.y - Graphics.boxHeight;
  var _xx = Graphics.boxWidth + _x / 2 * 3;
  var _yy = Graphics.boxHeight + _y / 2 * 3;
  if (_xx > 0 && _yy > 0 && _x > 0 && _y > 0) {
    if (this._spriteset._parallax) {
      this._spriteset._parallax.move(-1 * _x / 2, -1 * _y / 2, _xx, _yy);
      this._spriteset._parallax.origin.x -= _x / 2;
      this._spriteset._parallax.origin.y -= _y / 2;
    }
  }


  if (_xx > 0 && _yy > 0 && _x > 0 && _y > 0) {
    if (this._spriteset._overhead) {
      this._spriteset._overhead.move(-1 * _x / 2, -1 * _y / 2, _xx, _yy);
      this._spriteset._overhead.origin.x -= _x / 2;
      this._spriteset._overhead.origin.y -= _y / 2;
    }
  }

  // 如果存在地面层和远景层、存在镜头缩放，则远景尺寸锁定
  if ($gameMap.groundLayerName() !== '' && $gameMap.parallaxName()!=='' && $gameSystem.drill_LCa_curScaleX() !== 1) {
    this._spriteset._parallax.x = -_x/2;
    this._spriteset._parallax.y = -_y/2;
    this._spriteset._parallax.origin.x = 0;
    this._spriteset._parallax.origin.y = 0;
    this._spriteset._parallax.scale.x = 1 / $gameSystem.drill_LCa_curScaleX();
    this._spriteset._parallax.scale.y = 1 / $gameSystem.drill_LCa_curScaleY();
  }
}



//==============================
// * 隐藏莫名多出来的飞船阴影
//==============================
SWDA_MapCameraScale.Spriteset_Map_createShadow = Spriteset_Map.prototype.createShadow;
Spriteset_Map.prototype.createShadow = function () {
  SWDA_MapCameraScale.Spriteset_Map_createShadow.call(this);
  this._shadowSprite.opacity = 0;
};